﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Calc {
	public class Operation : IComputable {
		protected Operations operation;
		protected IComputable c1;
		protected IComputable c2;

		public Operation(IComputable czynnik1, Operations operation, IComputable czynnik2) {
			this.c1 = czynnik1;
			this.operation = operation;
			this.c2 = czynnik2;
		}

		public bool TryGetValue(out decimal result) {
			result = 0;

			decimal dc1;
			decimal dc2;

			if (!this.c1.TryGetValue(out dc1) || !this.c2.TryGetValue(out dc2))
				return false;

			switch (this.operation) {
				case Operations.Add:
					result = dc1 + dc2;
					return true;
				case Operations.Subtract:
					result = dc1 - dc2;
					return true;
				case Operations.Multiply:
					result = dc1 * dc2;
					return true;
				case Operations.Divide:
					result = dc1 / dc2;
					return true;
				default:
					result = 0;
					return false;
			}
		}

		public string GetEquation( ) {
			return this.c1.GetEquation( ) + " " + this.GetSymbol(this.operation) + this.c2.GetEquation( );
		}

		private string GetSymbol(Operations operations) {
			switch (operations) {
				case Operations.Add:
					return "+ ";
				case Operations.Subtract:
					return "- ";
				case Operations.Multiply:
					return "* ";
				case Operations.Divide:
					return "/ ";
				default:
					return "";
			}
		}
	}

	public enum Operations {
		Multiply,
		Divide,
		Add,
		Subtract
	}
}
